/*
   Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
   Original sources are available at www.latestbit.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package org.latestbit.smartobjects.utils;

import java.util.LinkedList;

public class PostActionListenersContainer<T> {
	public enum PostActionType {
		ADD,
		REMOVE
	}
	public class PostActionQueueItem {
		private T listener;
		private PostActionType postActionType;
		
		public PostActionQueueItem(T listener, PostActionType postActionType) {
			this.listener = listener;
			this.postActionType = postActionType;
		}

		public void setListener(T listener) {
			this.listener = listener;
		}

		public T getListener() {
			return listener;
		}

		public void setPostActionType(PostActionType postActionType) {
			this.postActionType = postActionType;
		}

		public PostActionType getPostActionType() {
			return postActionType;
		}
		
		public boolean isActionAdd() {
			return this.postActionType == PostActionType.ADD;
		}
		
		public boolean isActionRemove() {
			return this.postActionType == PostActionType.REMOVE;
		}
	}
	
	private LinkedList< PostActionQueueItem > postActionQueue= new LinkedList<PostActionQueueItem>();
	
	private boolean activated = false;

	protected void setActivated(boolean activated) {
		this.activated = activated;
	}

	public boolean isActivated() {
		return activated;
	}

	public LinkedList< PostActionQueueItem > getPostActionQueue() {
		return postActionQueue;
	}
	
	public void putListenerToAdd(T listener) {
		this.postActionQueue.add(new PostActionQueueItem(listener, PostActionType.ADD));
	}
	
	public void putListenerToRemove(T listener) {
		this.postActionQueue.add(new PostActionQueueItem(listener, PostActionType.REMOVE));
	}
	
	public void activate() {
		setActivated(true);
	}
	
	public void reset() {
		this.postActionQueue.clear();
		setActivated(false);
	}

	public void deactivate() {
		setActivated(false);
	}

	
}
